package com.wuchao.util;

import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author wuchao66
 * @Date 2023/8/25 15:10
 * @PackageName:com.wuchao.utils
 * @ClassName: StringToConvert
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
public class StringToConvert implements Converter<String, Date> {
    //定义一个私有的转换格式
    private String format;
    //用构造方法ioc快速注入容器
    public StringToConvert(String format){
        this.format=format;
    }//yyyy-MM-dd /HH:mm:ss
    @Override
    public Date convert(String s) {//s=="2020-09-08 ss..sds."
        DateFormat df = new SimpleDateFormat(this.format);
        Date parse =null;
        try {
             parse = df.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }
}
